package com.fire.dao.impl;

import com.fire.dao.ITeacherDao;
import com.fire.domain.Chance;
import com.fire.domain.Teacher;

import java.io.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;

public class TeacherDaoImpl implements ITeacherDao {
    @Override
    public LocalDateTime isHaveChance(String name, LocalDateTime now) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("chance.txt"));
        ArrayList<Chance> cList = new ArrayList<>();
        cList.clear();
        //  int count = 0;
        String s;
        while ((s = br.readLine()) != null) {//读文件,写入cList集合
            String[] split = s.split(",");
            Chance chance = new Chance(split[0], LocalDateTime.parse(split[1]));
            cList.add(chance);
        }
        br.close();
        ArrayList<Chance> list = new ArrayList<>();
        list.clear();
        for (int i = 0; i < cList.size(); i++) {//找出属于该用户名的记录，写入新集合并在原集合中删除
            if (cList.get(i).getName().equals(name)) {
                Chance chance = cList.get(i);
                cList.remove(i);
                list.add(chance);
                --i;
            }
        }
        /*System.out.println("一开始的" + list);
        System.out.println(list);*/
        BufferedWriter bw = new BufferedWriter(new FileWriter("chance.txt"));
        if (list.size() == 0 || list == null) {//若该用户是第一次输入错误，则将本次记录到文件中
            for (Chance chance1 : cList) {
                bw.write(chance1.getName() + "," + chance1.getDateTime());
                bw.newLine();
            }
            bw.write(name + "," + now);
            bw.newLine();
            bw.flush();
            bw.close();
            return now;
        } else {

            if (list.size() == 3) {//如果集合中已经有3组属于该用户的数据
                Duration duration1 = Duration.between(list.get(0).getDateTime(), now);
                Duration duration2 = Duration.between(list.get(1).getDateTime(), now);
                Duration duration3 = Duration.between(list.get(2).getDateTime(), now);
                long minutes1 = duration1.toMinutes();
                long minutes2 = duration2.toMinutes();
                long minutes3 = duration3.toMinutes();

                //System.out.println(minutes1);
                if (minutes3 > 20) {//若与最近的都大于20，则将本次操作记录到cList中，不在记录之前数据
                    //   System.out.println("都大于20分钟,两机会" + minutes2);
                    //  System.out.println("来到这了，Dao 64行");
                    Chance chance = new Chance(name, now);
                    cList.add(chance);

                    WriteCList(cList, bw);
                    return now;

                } else if (minutes2 > 20) {//如果与第二个的时间相差大于20，则说明与最远的时间也大于20，
                    // 将最近一次与本次添加到集合中
                    // System.out.println("存在3数，还有1");
                    cList.add(list.get(2));
                    Chance chance = new Chance(name, now);
                    cList.add(chance);
                    WriteCList(cList, bw);
                    LocalDateTime beforeTime = now.plusMinutes(20);
                    return beforeTime;

                } else if (minutes1 > 20) {//若只有最远一个大于20，则将本次与后两次的操作加入集合
                    //  System.out.println("3数，0机会");
                    cList.add(list.get(1));
                    cList.add(list.get(2));
                    Chance chance = new Chance(name, now);
                    cList.add(chance);
                    WriteCList(cList, bw);
                    LocalDateTime beforeTime = now.plusMinutes(40);
                    return beforeTime;


                } else {//到此说明都没超过20分钟，说明不能再输入了,不记录本次输入
                    /*System.out.println("20分钟限制" + minutes1);
                    System.out.println(now);
                    System.out.println(list.get(0).getDateTime());*/
                    cList.add(list.get(0));
                    cList.add(list.get(1));
                    cList.add(list.get(2));
                    /*Chance chance = new Chance(name, now);
                    cList.add(chance);*/
                    WriteCList(cList, bw);
                    LocalDateTime beforeTime = list.get(0).getDateTime().plusMinutes(60);
                    return beforeTime;

                }

            } else if (list.size() == 2) {//集合中存在两个数据
//                System.out.println("两数");
                Duration duration1 = Duration.between(list.get(0).getDateTime(), now);
                Duration duration2 = Duration.between(list.get(1).getDateTime(), now);
                long minutes1 = duration1.toMinutes();
                long minutes2 = duration2.toMinutes();
                if (minutes2 > 20) {//若与最近的都大于20，则将本次操作记录到clist中，不在记录之前数据
                    //   System.out.println(2);
                    Chance chance = new Chance(name, now);
                    cList.add(chance);

                    WriteCList(cList, bw);
                    return now;

                    /*for (int i = 0; i < list.size(); i++) {
                        cList.add(list.get(i));
                    }*/
                } else if (minutes1 > 20) {//若只有最远一个大于20，则将本次次与后最后一次的操作加入集合
                    //  System.out.println(1);
                    cList.add(list.get(1));
                    Chance chance = new Chance(name, now);
                    cList.add(chance);
                    WriteCList(cList, bw);
                    LocalDateTime beforeTime = now.plusMinutes(20);
                    return beforeTime;


                } else {//否则说明前两次都不大于20，将所有的都加入集合即可
                    //   System.out.println(0);
                    cList.add(list.get(0));
                    cList.add(list.get(1));
                    Chance chance = new Chance(name, now);
                    cList.add(chance);
                    WriteCList(cList, bw);
                    LocalDateTime beforeTime = now.plusMinutes(40);
                    return beforeTime;

                }

            } else {//只有一个数据
                //  System.out.println("1数");
                Duration duration = Duration.between(list.get(0).getDateTime(), now);
                long minutes = duration.toMinutes();
                if (minutes > 20) {//若大于20，写入本次
                    System.out.println(2);
                    Chance chance = new Chance(name, now);
                    cList.add(chance);
                    WriteCList(cList, bw);
                    return now;
                } else {
                    System.out.println(1);
                    cList.add(list.get(0));
                    Chance chance = new Chance(name, now);
                    cList.add(chance);
                    WriteCList(cList, bw);
                    LocalDateTime beforeTime = now.plusMinutes(20);
                    return beforeTime;
                }

            }
        }
    }

    @Override
    public int Login(String name, String password) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("users.txt"));
        ArrayList<Teacher> list = new ArrayList<>();
        String s;

        while ((s = br.readLine()) != null) {
            String[] split = s.split(",");
            Teacher tea = new Teacher(split[0], split[1]);
            list.add(tea);
        }
        String id = null;
        if (list.size() == 0 || list == null) {//文件夹为空
            return -2;
        } else {
            String ps = null;
            for (Teacher teacher : list) {
                id = teacher.getId();
                if (name.equals(id)) {
                    ps = teacher.getPassword();
                    break;
                }
            }
            if (!name.equals(id)) {//ID不存在
                return -1;
            }
            if (password.equals(ps)) {//正确
                return 1;
            } else {//密码错误
                return 0;
            }
        }

    }

    @Override
    public ArrayList<Teacher> findAll() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("users.txt"));
        ArrayList<Teacher> list = new ArrayList<>();
        String s;
        while ((s = br.readLine()) != null) {
            String[] split = s.split(",");
            Teacher tea = new Teacher(split[0], split[1]);
            list.add(tea);
        }
        br.close();
        return list;
    }

    @Override
    public void Register(String id, String p1) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("users.txt", true));
        bw.write(id + "," + p1);
        bw.newLine();
        bw.close();
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("registfile.txt", true));
        LocalDateTime now = LocalDateTime.now();
        bufferedWriter.write(id + "," + now);
        bufferedWriter.newLine();
        bufferedWriter.close();
    }

    @Override
    public LocalDateTime WriterAndClear(String name, LocalDateTime now) throws IOException {
        //因为登录成功了，所以要删除该用户之前的错误登录记录
        BufferedReader brc = new BufferedReader(new FileReader("chance.txt"));
        ArrayList<Chance> list = new ArrayList<>();
        String s;
        while ((s = brc.readLine()) != null) {
            String[] split = s.split(",");
            Chance chance = new Chance(split[0], LocalDateTime.parse(split[1]));
            list.add(chance);
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getName().equals(name)) {
                list.remove(i);
                i--;
            }

        }
        BufferedWriter bwc = new BufferedWriter(new FileWriter("chance.txt"));
        WriteCList(list, bwc);//写回文件
        //将本次登录记录到操作登录日志，并将上一次登录时间返回
        BufferedReader br1 = new BufferedReader(new FileReader("loginfiles.txt"));
        ArrayList<Chance> arrayList = new ArrayList<>();
        String s2;
        while ((s2 = br1.readLine()) != null) {//读文件,写入cList集合
            String[] split = s2.split(",");
            Chance chance = new Chance(split[0], LocalDateTime.parse(split[1]));
            arrayList.add(chance);
        }
        br1.close();
        if (arrayList.size() == 0 || arrayList == null) {
            Chance chance = new Chance(name, now);
            arrayList.add(chance);
            return now;
        }
        boolean flag = false;
        LocalDateTime dateTime = now;
        lo:
        for (int i = 0; i < arrayList.size(); i++) {
            dateTime = arrayList.get(i).getDateTime();
            if (arrayList.get(i).getName().equals(name)) {//如果存在该用户的记录就返回上次登录时间
                flag = true;

                arrayList.remove(i);
                break lo;
            }
        }
        if (flag) {
            Chance chance = new Chance(name, now);
            arrayList.add(chance);
            BufferedWriter bw1 = new BufferedWriter(new FileWriter("loginfiles.txt"));
            WriteCList(arrayList, bw1);
            //    System.out.println("运行1");
            return dateTime;

        } else {

            BufferedWriter bw1 = new BufferedWriter(new FileWriter("loginfiles.txt"));
            Chance chance = new Chance(name, now);
            arrayList.add(chance);
            WriteCList(arrayList, bw1);
            //    System.out.println("运行2");
            return now;

        }

    }

    @Override
    public LocalDateTime CloseTime(String name) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("closetime.txt"));
        ArrayList<Chance> list = new ArrayList<>();
        String s;
        while ((s = br.readLine()) != null) {
            String[] split = s.split(",");
            Chance chance = new Chance(split[0], LocalDateTime.parse(split[1]));
            list.add(chance);
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getName().equals(name)) {
                return list.get(i).getDateTime();
            }
        }
        return null;
    }

    @Override
    public LocalDateTime RegisterTime(String name) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("registfile.txt"));
        ArrayList<Chance> list = new ArrayList<>();
        String s;
        while ((s = br.readLine()) != null) {
            String[] split = s.split(",");
            Chance chance = new Chance(split[0], LocalDateTime.parse(split[1]));
            list.add(chance);
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getName().equals(name)) {
                return list.get(i).getDateTime();
            }
        }
        return null;
    }

    @Override
    public void WriteCloseTime(String name) throws IOException {
        LocalDateTime now = LocalDateTime.now();
        BufferedReader br1 = new BufferedReader(new FileReader("closetime.txt"));
        ArrayList<Chance> arrayList = new ArrayList<>();
        String s2;
        while ((s2 = br1.readLine()) != null) {//读文件,写入cList集合
            String[] split = s2.split(",");
            Chance chance = new Chance(split[0], LocalDateTime.parse(split[1]));
            arrayList.add(chance);
        }
        br1.close();
        for (int i = 0; i < arrayList.size(); i++) {//删除上一次的时间
            if(arrayList.get(i).getName().equals(name)){
                arrayList.remove(i);
                break;
            }
        }
        Chance chance = new Chance(name, now);
        arrayList.add(chance);
        BufferedWriter bw = new BufferedWriter(new FileWriter("closetime.txt"));
        WriteCList(arrayList,bw);
    }


    private void WriteCList(ArrayList<Chance> cList, BufferedWriter bw) throws IOException {
        for (Chance chance : cList) {
            bw.write(chance.getName() + "," + chance.getDateTime());
            bw.newLine();
        }
        bw.flush();
        bw.close();
    }
}
